/*
 * UpdateNewBooks.fx
 * Author: Yu Min-ting
 * Created on 2009-7-13, 16:20:32
 */

package com.sjtu.fx.admin;

import javafx.scene.*;
import javafx.stage.Stage;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.scene.layout.*;
import javafx.scene.control.TextBox;


import com.sjtu.fx.tools.Panel;

import javafx.scene.control.Slider;
import javafx.ext.swing.SwingComboBox;
import javafx.ext.swing.SwingComboBoxItem;
import javafx.scene.control.Label;
import javafx.scene.control.Button;

import javafx.stage.Alert;
import javafx.scene.control.Hyperlink;

import javafx.io.http.HttpRequest;
import java.io.DataInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.json.JSONObject;

import java.lang.System;

import javafx.geometry.HPos;

import com.sjtu.fx.tools.UIAlert;

public class UpdateNewBooks extends CustomNode{
    var panel:Panel;
    var categoryManagement:CategoryManagement;

    var bookname:String= bind nameTextBox.text;
    var author:String = bind authorTextBox.text;
    var description:String = bind despTextBox.text;
    var coverURL:String = bind coverTextBox.text;
    var category:String = bind categoryTextBox.text;
    var isbn:String = bind isbnTextBox.text;
    var publisher:String = bind publisherTextBox.text;
    var bookScore:String = bind (scoreSlider.value as Integer).toString();
    var label1:String = bind keywordTextBox1.text;
    var label2:String = bind keywordTextBox2.text;
    var label3:String = bind keywordTextBox3.text;
    var label4:String = bind keywordTextBox4.text;
    var label5:String = bind keywordTextBox5.text;
    var label6:String = bind keywordTextBox6.text;
    var libraryAddress:String = bind libraryBox.text;
    var bookNumber:String = bind (numberSlider.value as Integer).toString();

    public var needScore:Number;
    public var number:Number;

    var nameText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book Name: "
    };
    var nameTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

     var isbnText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book ISBN:"
    };
    var isbnTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

    var authorText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book Author: "
    };
    var authorTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

    var publisherText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book Publisher: "
    };
    var publisherTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

    var keywordText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book Keyword: "
    };
    var keywordTextBox1 = TextBox {
        columns:7
        blocksMouse:false
    }
    var keywordTextBox2 = TextBox {
        columns:7
        blocksMouse:false
    }
    var keywordTextBox3 = TextBox {
        columns:7
        blocksMouse:false
    }
    var keywordTextBox4 = TextBox {
        columns:7
        blocksMouse:false
    }
    var keywordTextBox5 = TextBox {
        columns:7
        blocksMouse:false
    }
    var keywordTextBox6 = TextBox {
        columns:7
        blocksMouse:false
    }

    var scoreText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Check in Score: "
    };
    var scoreSlider = Slider {
            min : 0
            max: 10
            vertical: false
            value : bind needScore with inverse
        }
    var scoreLabel = Label {
            text: bind ({needScore} as Integer).toString() ;
        }

    var numberText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book Quantity : "
    };
    var numberSlider = Slider {
            min : 0
            max: 5
            vertical: false
             value : bind number with inverse
        }
    var numberLabel = Label {
            text: bind ({number} as Integer).toString() ;
        }

    var libraryText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book Location : "
    };
    var libraryBox = SwingComboBox {
            items: [
                SwingComboBoxItem {
                    text: "Library1"
                    selected: true
                }
                SwingComboBoxItem {
                    text: "Library2"
                }
                SwingComboBoxItem {
                    text: "Library3"
                }
            ]
        }

    var despText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Description: "
    };
    var despTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

    var coverText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Cover URL: "
    };
    var coverTextBox = TextBox {
        columns:27
        blocksMouse:false
    }

     var categoryText = Text {
        font: Font {size:10 name: "Verdana"}
        fill: Color.rgb(11,51,60)
        content: "Book Category: "
    };
    public var categoryTextBox = TextBox {
        columns:27
        blocksMouse:false
        editable: false
    }

    var categoryChooser = Hyperlink {
        translateX: 240 translateY:420
        text: "Book Category Selection "
        font: Font {size:30 name: "Verdana Bold"}
        action: function() {
            categoryManagement.update = this;
            categoryManagement.visible = true;

        }
    }

     var alert = UIAlert {
         translateX:75
         translateY:200
         title:"ERROR"
         content:"Please input entire information"
         width:280
         height:120
         visible:false
         opacity:0.9
     }
   var complete = Button {
        text: "Complete"
        translateX:180 translateY:490
        action: function() {
            if(nameTextBox.text==""or authorTextBox.text==""
                     or isbnTextBox.text=="" or publisherTextBox.text=="")
                {
                    alert.visible = true;
                 //Alert.inform("ERROR","Please input entire information");
                 
                }
            else{
                addBook();
                alert.title = "Success";
                alert.content = "Update new books successfully!";
                alert.visible = true;
                //Alert.inform("Success","Update new books successfully!");
                visible = false;
                this.parent.lookup("topBooks").visible = true;
                this.parent.lookup("recommendBooks").visible = true;
            }


        }
    }




    override protected function create() : Node {
        Group{
            content:[
                 panel = Panel {
                    width:    430
                    height:   550
                    title:    "Update New Books"
                    translateX: 0
                    translateY: 0
                },
                VBox{
                    spacing: 20
                    translateX:40 translateY: 55
                    content:[
                        nameText, isbnText, authorText, publisherText,
                        despText, coverText , keywordText
                        ]
                },
                 VBox{
                    spacing: 8
                    translateX:150 translateY: 50
                    content:[
                        nameTextBox, isbnTextBox, authorTextBox, publisherTextBox,
                        despTextBox, coverTextBox,
                        Flow{
                            width:250
                            height:80
                            hgap:4
                            vgap:10
                            hpos:HPos.LEFT
                            content:[keywordTextBox1,keywordTextBox2,keywordTextBox3,
                            keywordTextBox4,keywordTextBox5,keywordTextBox6]
                        }
                        ]
                },
                HBox{
                    spacing:55
                    translateX:40 translateY: 320
                    content:[scoreText,scoreSlider,scoreLabel]
                },
                 HBox{
                    spacing:55
                    translateX:40 translateY: 350
                    content:[numberText,numberSlider,numberLabel]
                },
                 HBox{
                    spacing:100
                    translateX:40 translateY: 450
                    content:[libraryText,libraryBox]
                },
                HBox{
                    spacing:20
                    translateX:40 translateY:380
                    content:[categoryText,categoryTextBox]
                    
                },categoryChooser, complete,
                categoryManagement = CategoryManagement{

                    visible: false
                    }
                 alert


            ]
        }
     }

function run() {
    Stage {
       width: 420 height: 520
        scene: Scene {
            content: [
                UpdateNewBooks {translateX: 40 translateY: 40}
            ]
        }
    }
}
function addBook(){
    HttpRequest {
        method:HttpRequest.POST
        location:"http://localhost:8764/JavaFXBack/AddBook"

       onOutput: function(os:OutputStream) {
            try {
                var json:JSONObject = JSONObject{};
                json.put("bookname",nameTextBox.text);
                json.put("author",authorTextBox.text);
                json.put("isbn",isbnTextBox.text);
                json.put("publisher",publisherTextBox.text);
                json.put("label1",keywordTextBox1.text);
                json.put("label2",keywordTextBox2.text);
                json.put("label3",keywordTextBox3.text);
                json.put("label4",keywordTextBox4.text);
                json.put("label5",keywordTextBox5.text);
                json.put("label6",keywordTextBox6.text);
                json.put("description",despTextBox.text);
                json.put("coverURL",coverTextBox.text);
                json.put("bookScore",scoreLabel.text);
                json.put("number",numberLabel.text);
                json.put("library",libraryBox.text);
                json.put("category",categoryTextBox.text );
                var temp:String = "obj={json.toString()}";
                os.write(temp.getBytes());
                os.flush();
            } finally {
                os.close();
            }
        }

        onInput: function(is: InputStream) {
            try {
                def data:DataInputStream = new DataInputStream(is);
            //    var json:JSONObject = JSONObject{};
                var result:String = data.readLine();
                System.out.println(result);
            }finally {
                is.close();
            }
        }
    }.start();
}

}
